const { resolve } = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack')
module.exports = {
  entry: './src/ts/index.ts',   // 打包对入口文件，期望打包对文件入口。 
  output: {
    filename: 'index.js',   // 输出文件名称
    path: resolve(__dirname, './build')  //获取输出路径
  },
  mode: 'development',
  target: 'node',
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new HtmlWebpackPlugin({
      template: './src/popup.html',
      filename: 'popup.html',
    }),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: resolve(__dirname, "./src/img"),
          to: resolve(__dirname, "./build/img"),
        },
        {
          from: resolve(__dirname, "./src/manifest.json"),
          to: resolve(__dirname, "./build/manifest.json"),
        }
      ]
    })
  ],
  module: {
    rules: [{
      test: /\.tsx?$/,
      use: 'ts-loader',
      exclude: /node_modules/
    }]
  },
  resolve: {
    extensions: ['.ts']      // 解析对文件格式
  },
  devServer: {
    //运行的端口号
    port: 9000,
    //目的地址
    contentBase:'./src',
    //启动gzip压缩
    compress: true,
    filename: './src/ts/index.js',
    writeToDisk: (filename) => {
      return /tsc_out.js/.test(filename);
    }
  }
}
